package com.zlc.platform.service.impl;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlc.common.common.constant.ParameterType;
import com.zlc.common.common.exception.BusinessException;
import com.zlc.common.core.sys.model.vo.MayanPlatParameterVo;
import com.zlc.common.core.sys.service.MayanPlatParameterService;
import com.zlc.common.util.tree.TreeUtil;
import com.zlc.platform.model.SysParameterEntity;
import com.zlc.platform.service.SysParameterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SysParameterServiceImpl implements SysParameterService {

    @Autowired
    private MayanPlatParameterService mayanPlatParameterService;

    @Override
    public List<SysParameterEntity> queryParameterList() {
        List<MayanPlatParameterVo> parameterVos = mayanPlatParameterService.queryVoList(new LambdaQueryWrapper<>());
        //查询分类
        List<String> catalogList = parameterVos.stream().map(MayanPlatParameterVo::getCatalog).distinct().collect(Collectors.toList());
        //组装
        List<SysParameterEntity> entityList = new ArrayList<>();

        catalogList.forEach(catalog -> {
            List<String> models = StrUtil.split(catalog, "-");
            String parent = "";
            List<String> treeModels = new ArrayList<>();
            for(String model : models){
                treeModels.add(model);
                String newCatalog = StrUtil.join("-", treeModels);

                SysParameterEntity entity = entityList.stream().filter(e -> e.getCatalog().equals(newCatalog)).findFirst().orElse(null);
                if(ObjectUtil.isEmpty(entity)){
                    entity = new SysParameterEntity();
                    entity.setCode(newCatalog);
                    entity.setParentCode(parent);
                    entity.setParameterCatalog(model);
                    entity.setSortNum(model.hashCode());
                    entity.setCatalog(newCatalog);
                    entityList.add(entity);
                }
                parent = newCatalog;
                List<MayanPlatParameterVo> parameters = parameterVos.stream().filter(p -> p.getCatalog().contentEquals(newCatalog))
                        .sorted(Comparator.comparing(MayanPlatParameterVo::getSortNum)).collect(Collectors.toList());
                entity.setParameterVos(parameters);
            }
        });
        return TreeUtil.buildTree(entityList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeParameters(List<MayanPlatParameterVo> parameterVos) {
        for(MayanPlatParameterVo parameterVo : parameterVos){
            if(ObjectUtil.isEmpty(parameterVo.getId())){
                continue;
            }
            MayanPlatParameterVo existParameter = mayanPlatParameterService.queryOne(parameterVo.getId());
            if(!ObjectUtil.isEmpty(existParameter) && !ObjectUtil.isEmpty(parameterVo.getDefaultValue())){
                checkDefaultValue(parameterVo);
                existParameter.setDefaultValue(parameterVo.getDefaultValue());
                mayanPlatParameterService.saveVo(existParameter);
            }
        }
    }

    private void checkDefaultValue(MayanPlatParameterVo parameterVo){
        String defaultValue = parameterVo.getDefaultValue();
        String type = parameterVo.getType();
        if(ParameterType.INTEGER.name().equals(type)){
            //整数
            if(!NumberUtil.isInteger(defaultValue)){
                throw new BusinessException("参数["+parameterVo.getParameterKey()+"]请输入整数!");
            }
        }
    }
}
